//
//  WellcomeBackViewController.swift
//  weibo1
//
//  Created by nick on 15/9/26.
//  Copyright © 2015年 nick. All rights reserved.
//

import UIKit
import SnapKit

class WellcomeBackViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.whiteColor()
        setupUI()
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        iconView.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(-bgImageView.bounds.height + 160)
        }


        UIView.animateWithDuration(1, delay: 0, options: [], animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(1, animations: { () -> Void in
                    self.wellcomLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        NSNotificationCenter.defaultCenter().postNotificationName(WBChangeVCNotificationKey, object: nil)
                })
        }
        
    }
    
    private func setupUI() {
        // 添加控件
        view.addSubview(bgImageView)
        bgImageView.addSubview(wellcomLabel)
        bgImageView.addSubview(iconView)
        wellcomLabel.alpha = 0
        // 布局控件
        bgImageView.frame = view.bounds

//        wellcomLabel.snp_makeConstraints { (make) -> Void in
//            make.centerX.equalTo(bgImageView.snp_centerX)
//            make.bottom.equalTo(bgImageView.snp_bottom).offset(-160)
//        }
//        iconView.snp_makeConstraints { (make) -> Void in
//            make.centerX.equalTo(wellcomLabel.snp_centerX)
//            make.bottom.equalTo(wellcomLabel.snp_top).offset(-16)
//        }
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(bgImageView.snp_centerX)
            make.bottom.equalTo(bgImageView.snp_bottom).offset(-160)
            make.width.equalTo(90)
            make.height.equalTo(90)
        }
        wellcomLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_centerX)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
    }
 
    // 懒加载控件
    /// 背景图片
    lazy var bgImageView: UIImageView = {
        let bgView = UIImageView(image: UIImage(named: "ad_background"))
//        bgView.frame = self.view.bounds
        return bgView
    }()
    /// 欢迎回来标签
    lazy var wellcomLabel: UILabel = UILabel(title: "欢迎回来", color: UIColor.darkGrayColor(), fontSize: 18)
    /// 头像
    lazy var iconView: UIImageView = {
        let iconView = UIImageView(image: UIImage(named: "avatar_default_big"))
        iconView.layer.cornerRadius = 45
        iconView.layer.masksToBounds = true
        return iconView
    }()
}
